import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../views/Login.vue'
import Home from '../views/Home.vue'
// import Welcome from '../components/Welcome.vue'
// import Users from '../system/users/index.vue'
// import Roles from '../system/roles/role.vue'
// import DataInsert from '../views/datainsert.vue'
// import Report from '../views/report/index.vue'
// import FormalCustomer from '../views/formalcustomercomplaintReport/index.vue'
// import Files from '../views/filles/index.vue'
// //用户信息
// import userinformation from '../views/userinformation/index.vue'
// //树形菜单
// import treemenu from '../views/treemenu/index.vue'
// //树形菜单table
// import treetablemenu from '../views/treemenutable/table.vue'
// //父组件
// import father from '../views/father/father.vue'
// import menu from '../views/menu/index.vue'
import { getUserTreeMenu } from "../api/users";
Vue.use(VueRouter)


const routes = [
  {
    path: '/',
    name: 'loge',
    component: Login
  },
  {
    path: '/home',
    name: 'home',
    component: Home,
    // redirect: '/welcome',
    children: [
      //   {
      //   path: '/welcome',
      //   name: 'welcome',
      //   component: Welcome,
      //   meta:{requireAuth:true}
      // },
      // { path: '/users', name: 'users', component: Users,meta:{requireAuth:true} },
      // { path: '/roles', name: 'roles', component: Roles },
      // { path: '/test1', name: 'test1', component: () => import(/* webpackChunkName: "about" */ '../views/Test.vue') },
      // //数据录入
      // { path: '/datainsert', name: 'datainsert', component: DataInsert },
      // //报表
      // { path: '/report', name: 'datainsert', component: Report },
      // { path: '/formalCustomer', name: 'formalCustomer', component: FormalCustomer },
      // { path: '/files', name: 'files', component: Files },
      // { path: '/userinformation', name: 'userinformation', component: userinformation },
      // { path: '/treemenu', name: 'treemenu', component: treemenu },
      // { path: '/treetablemenu', name: 'treetablemenu', component: treetablemenu },
      // { path: '/father', name: 'father', component: father },
      // { path: '/menu', name: 'menu', component: menu },
    ]
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    // component: () => import(/* webpackChunkName: "about" */ '../views/Home.vue'),
  },
  // {
  //   path: '/test1',
  //   name: 'test1',
  //   component: () => import(/* webpackChunkName: "about" */ '../views/Test.vue')
  // }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
router.beforeEach((to, from, next) => {
  if (to.path === "/") {
    next()
  } else {
    let data = [];
    // const res=getUserTreeMenu(window.localStorage.getItem("userCode"));
    //console.log(res.data);
    //发送请求获取数据
    getUserTreeMenu(window.localStorage.getItem("userCode")).then(res => {
      console.log('menulist', res.menulist)
      data = res.menulist;
      console.log('123', data)
      let addDynamicRoute = dynamicRouths(data);
      console.log('addDynamicRoute', addDynamicRoute)
      router.addRoute(addDynamicRoute);



    });
    next()

  }
})
function dynamicRouths(data) {
  console.log('data1', data)
  //根据数据动态生成路由
  let HomeRoute = routes.filter(it => it.path == '/home')[0];
  HomeRoute.children = [];
  data.forEach(item => {
    HomeRoute.children.push({
      path: item.path,
      name: item.menuCode,
      //  component: () => import(`../views/menu/index.vue`)
       component:()=>import(`@/views${item.path}/index.vue`)
    })
  })
  return HomeRoute;
}
export default router
